package logic

import (
	"context"
	"errors"
	"zc1/model/model"

	"zc1/zc1rpc/internal/svc"
	"zc1/zc1rpc/zc1rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}
func (l *AddLogic) Add(in *zc1rpc.AddRequest) (*zc1rpc.AddResponse, error) {
	// todo: add your logic here and delete this line

	var user model.User

	model.DB.Debug().Where("phone=?", in.Phone).First(&user)

	if user.Phone == in.Phone {
		return nil, errors.New("用户已注册")
	}

	user = model.User{
		Phone:    in.Phone,
		Password: in.Password,
	}
	model.DB.Debug().Create(&user)

	return &zc1rpc.AddResponse{
		UserId: int64(user.ID),
	}, nil
}
